//
//  LYYTabBarButton.m
//  LYYWeibo
//
//  Created by lanou3g on 15/10/10.
//  Copyright (c) 2015年 李岩岩. All rights reserved.

//图标的比例
#define LYYTabBarButtonImageRatio 0.6

// 按钮的默认文字颜色
#define  LYYTabBarButtonTitleColor [UIColor whiteColor]
// 按钮的选中文字颜色
#define LYYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define  LYYTabBarButtonTitleSelectedColor LYYColor(3, 189, 249)
//03bdf9

#import "LYYTabBarButton.h"
#import "UIImage+LYY.h"
#import "LYYBadgeButton.h"

@interface LYYTabBarButton ()
/**
 *  提醒数字
 */

@property (nonatomic, strong) LYYBadgeButton *badgeButton;
@end

@implementation LYYTabBarButton
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 图标居中
        self.imageView.contentMode = UIViewContentModeCenter;
        // 文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 字体大小
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        // 文字颜色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:LYYTabBarButtonTitleSelectedColor forState:UIControlStateSelected];
        
        
        //添加一个提醒数字按钮
        LYYBadgeButton *badgeButton = [[LYYBadgeButton alloc]init];
        [badgeButton setBackgroundImage:[UIImage resizedImageWithName:@"main_badge_os7"] forState:UIControlStateNormal];
        
        [self addSubview:badgeButton];
    
        self.badgeButton = badgeButton;
        
        badgeButton.titleLabel.font = [UIFont systemFontOfSize:11];
        
    }
    return self;
}

// 重写去掉高亮状态
- (void)setHighlighted:(BOOL)highlighted {}

// 内部图片的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = contentRect.size.height * LYYTabBarButtonImageRatio;
    return CGRectMake(0, 0, imageW, imageH);
}

// 内部文字的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = contentRect.size.height * LYYTabBarButtonImageRatio;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height - titleY;
    return CGRectMake(0, titleY, titleW, titleH);
}

// 设置item
- (void)setItem:(UITabBarItem *)item
{
    _item = item;
    //设置文字
    [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];
    [item addObserver:self forKeyPath:@"title" options:0 context:nil];
    [item addObserver:self forKeyPath:@"image" options:0 context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil];
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
   
}
- (void)dealloc
{
    [self.item removeObserver:self forKeyPath:@"badgeValue"];
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedImage"];
}
/**
 *  监听到某个对象的属性改变了,就会调用
 *
 *  @param keyPath 属性名
 *  @param object  哪个对象的属性被改变
 *  @param change  属性发生的改变
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    // 设置文字
    [self setTitle:self.item.title forState:UIControlStateSelected];
    [self setTitle:self.item.title forState:UIControlStateNormal];
    
    // 设置图片
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];
    
    
    // KVO 监听属性改变
    if (self.item.badgeValue) {
        self.badgeButton.hidden = NO;
        //设置文字
        [self.badgeButton setTitle:self.item.badgeValue forState:UIControlStateNormal];
        
        //设置frame
        
        CGFloat badgeY = 5;
        
        CGFloat badgeH = self.badgeButton.currentBackgroundImage.size.width;
        
        NSDictionary *attribute = @{NSFontAttributeName: self.badgeButton.titleLabel.font};
        CGSize badgeSize = [self.item.badgeValue sizeWithAttributes:attribute];
        
//        CGSize badgeSize = [self.item.badgeValue sizeWithFont:self.badgeButton.titleLabel.font];
        
        CGFloat badgeW = badgeSize.width + 15;
        
        CGFloat badgeX = self.badgeButton.currentBackgroundImage.size.width + 22;
        
        
        self.badgeButton.frame = CGRectMake(badgeX, badgeY, badgeW, badgeH);
        
    }else{
        self.badgeButton.hidden = YES;
    }

    
   
    
    //设置提醒数字
    
    
}




@end
